package com.example.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.domain.entity.RestBean;
import com.example.backend.domain.po.Complaints;
import com.example.backend.service.IComplaintsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyy
 * @since 2025-05-27
 */
@RestController
@RequestMapping("/complaints")
public class ComplaintsController {
    @Autowired
    private IComplaintsService complaintsService;

    // 根据 ID 查询单个投诉记录
    @GetMapping("/{id}")
    public RestBean<Complaints> getComplaintById(@PathVariable Integer id) {
        Complaints complaint = complaintsService.getById(id);
        return RestBean.success(complaint);
    }

    // 根据多个 ID 查询投诉记录
    @GetMapping("/batch")
    public RestBean<List<Complaints>> getComplaintsByIds(@RequestParam List<Integer> ids) {
        List<Complaints> complaints = complaintsService.listByIds(ids);
        return RestBean.success(complaints);
    }

    // 根据条件查询投诉记录
    @GetMapping("/search")
    public RestBean<List<Complaints>> searchComplaints(
            @RequestParam(required = false) Integer customerId,
            @RequestParam(required = false) String status) {
        LambdaQueryWrapper<Complaints> queryWrapper = new LambdaQueryWrapper<>();
        if (customerId != null) {
            queryWrapper.eq(Complaints::getCustomerId, customerId);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq(Complaints::getStatus, status);
        }
        List<Complaints> complaints = complaintsService.list(queryWrapper);
        return RestBean.success(complaints);
    }

    // 分页查询投诉记录
    @GetMapping("/page")
    public RestBean<IPage<Complaints>> getComplaintsByPage(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) Integer customerId,
            @RequestParam(required = false) String status) {
        Page<Complaints> page = new Page<>(current, size);
        LambdaQueryWrapper<Complaints> queryWrapper = new LambdaQueryWrapper<>();
        if (customerId != null) {
            queryWrapper.eq(Complaints::getCustomerId, customerId);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq(Complaints::getStatus, status);
        }
        IPage<Complaints> complaintPage = complaintsService.page(page, queryWrapper);
        return RestBean.success(complaintPage);
    }

    // 新增投诉记录
    @PostMapping
    public RestBean<Void> addComplaint(@RequestBody Complaints complaint) {
        boolean save = complaintsService.save(complaint);
        return RestBean.success();
    }

    // 更新投诉记录信息
    @PutMapping("/{id}")
    public RestBean<Void> updateComplaint(
            @PathVariable Integer id,
            @RequestBody Complaints complaint) {
        complaint.setId(id);
        boolean update = complaintsService.updateById(complaint);
        return RestBean.success();
    }

    // 删除投诉记录
    @DeleteMapping("/{id}")
    public RestBean<Void> deleteComplaint(@PathVariable Integer id) {
        boolean remove = complaintsService.removeById(id);
        return RestBean.success();
    }
}
